(import "././login/env.inc")
(import "gui/lisp.inc")

(defq id :t index 0 xv 4 yv 0 i 512
	+frames `',(exec '(map (# (canvas-load (cat "apps/freeball/data/staoball_" (str %0) ".cpm") +load_flag_shared)) (range 1 13)))
	+sframes `',(exec '(map (# (canvas-load (cat "apps/freeball/data/staoball_s_" (str %0) ".cpm") +load_flag_shared)) (range 1 13))))

(ui-root view (View) (:color 0)
	(ui-element frame (first +frames))
	(ui-element sframe (first +sframes)))

(defun main ()
	(defq screen (penv (gui-add-front-rpc view)))
	(while id
		(bind '(_ _ screen_width screen_height) (. screen :get_bounds))
		(defq index (% (inc index) (length +frames))
			old_frame frame frame (elem-get +frames index)
			old_sframe sframe sframe (elem-get +sframes index))
		(bind '(ox oy w h) (. view :get_bounds))
		(bind '(_ _ fw fh) (. old_frame :get_bounds))
		(bind '(_ _ sw sh) (. old_sframe :get_bounds))
		(defq x (+ ox xv) y (+ oy yv) yv (inc yv))
		(if (> y (- screen_height fh)) (setq y (- screen_height fh) yv (neg (/ (* yv 8) 10))))
		(if (< x 0) (setq x 0 xv (abs xv)))
		(if (> x (- screen_width fw)) (setq x (- screen_width fw) xv (neg (abs xv))))
		(. frame :set_bounds 0 0 fw fh)
		(. sframe :set_bounds 8 32 sw sh)
		(. old_sframe :sub)
		(. old_frame :sub)
		(.-> view (:add_back sframe) (:add_front frame)
			(:change_dirty x y (+ 8 sw) (+ 32 sh)))
		(setq id (/= 0 (-- i)))
		(while (mail-poll (list (task-mbox)))
			(setq id (getf (defq msg (mail-read (task-mbox))) +ev_msg_target_id))
			(if (or (= id 0)
					(and (< id 0)
						(= (getf msg +ev_msg_type) +ev_type_mouse)
						(/= (getf msg +ev_msg_mouse_buttons) 0)))
				(setq id :nil)))
		(task-sleep 40000))
	(gui-sub-rpc view))
